<script setup>
import SvgIcon from "@/components/SvgIcon/index.vue";

import { getCodeImg, login } from "@/network/interface/login.js";

import router from "@/router/index";

import { setToken } from "@/utils/auth";

import Cookies from "js-cookie";

// 定义表单相关的变量并设置初始值
const loginForm = ref({
  username: "admin",
  password: "admin123",
  rememberMe: false,
  code: "",
  uuid: "",
});

// 验证码
const codeUrl = ref("");

// 验证码开关
const captchaEnabled = ref(true);

function getCode() {
  getCodeImg().then((res) => {
    console.log(res);
    captchaEnabled.value =
      res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      loginForm.value.uuid = res.uuid;
    }
  });
}

getCode();
// console.log(import.meta.env.VITE_APP_BASE_API) // dev-api
// console.log(import.meta.env.VITE_APP_ENV) // development
const loginRef = ref(null);
// 登录
function handleLogin(loginRef) {
  loginRef.validate((valid) => {
    if (!valid) {
      return ElMessage.error("请填写登录信息或同意协议再进行登录操作！");
    }
  });
  login(
    loginForm.value.username,
    loginForm.value.password,
    loginForm.value.code,
    loginForm.value.uuid
  ).then((res) => {
    console.log(res);
    // console.log(typeof res);
    // let resp = JSON.parse(res);
    let resp = res;
    if (200 === resp.code) {
      console.log("登录成功");
      //   保存token
      setToken(resp.token);
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }

      router.push({ path: "/index" });
    }
  });
}

// 表单验证规则
const formRules = {
  username: [
    {
      required: true,
      message: "用户名不能为空",
      trigger: "blur",
    },
    {
      pattern: /^\w{3,8}$/,
      message: "请输入有效的用户名（3位，数字字母或下划线)",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
    {
      pattern: /^[a-zA-Z0-9@#$%&]{3,8}$/,
      message: "请输入有效的用户名（3位，数字字母或下划线",
      trigger: "blur",
    },
  ],
  code: [
    {
      required: true,
      message: "请输入验证码",
      trigger: "blur",
    },
  ],
};
</script>

<template>
  <svg-icon name="order" class="icon"></svg-icon>
  <div class="login">
    <el-form
      ref="loginRef"
      class="login-form"
      :model="loginForm"
      :rules="formRules"
    >
      <h3 class="title">若依后台管理系统</h3>
      <el-form-item prop="username">
        <el-input
          type="text"
          size="large"
          auto-complete="off"
          placeholder="账号"
          v-model="loginForm.username"
        >
          <template #prefix>
            <svg-icon icon-class="user" class="el-input__icon input-icon" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          size="large"
          auto-complete="off"
          placeholder="密码"
          v-model="loginForm.password"
        >
          <template #prefix>
            <svg-icon icon-class="password" class="el-input__icon input-icon" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          size="large"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          v-model="loginForm.code"
        >
          <template #prefix>
            <svg-icon
              icon-class="validCode"
              class="el-input__icon input-icon"
            />
          </template>
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      <el-checkbox
        v-model="loginForm.rememberMe"
        style="margin: 0px 0px 25px 0px"
        >记住密码</el-checkbox
      >
      <el-form-item style="width: 100%">
        <el-button
          size="large"
          type="primary"
          style="width: 100%"
          @click="handleLogin(loginRef)"
        >
          <span>登 录</span>
        </el-button>
        <div style="float: right">
          <!--<router-link class="link-type" :to="'/register'">立即注册</router-link>-->
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<style lang="scss"  scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 40px;

    input {
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
